<?php
/**
 * Created by PhpStorm.
 * User: zc
 * Date: 2019/8/4
 * Time: 14:36
 */
namespace app\api\controller\v1;

use app\common\controller\Api;
use app\common\logic\PersonalM;
use think\db;

class Personal extends Api
{
    protected $noNeedLogin = [];  //不能放*
    protected $noNeedRight = '*';
    /*
     * 获取我的问答接口
     */
    public function myQuestion()
    {
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $model = new PersonalM();
        $data = $model->setUserId(UID)->setOrgCateId(ORG_CATE_ID)->setPage($page)->setPagesize($pagesize)->getAskList();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
    /*
     * 获取我的测评接口
     */
    public function myEvaluation()
    {
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $model = new PersonalM();
        $data = $model->setUserId(UID)->setOrgCateId(ORG_CATE_ID)->setPage($page)->setPagesize($pagesize)->getEvaluationList();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
    /*
     * 获取我的咨询接口
     */
    public function myConsult()
    {
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $type = $this->request->post('type')?:1;
        $model = new PersonalM();
        $data = $model->setUserId(UID)->setPage($page)->setPagesize($pagesize)->getConsultList($type);
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
    /*
     * 获取老师介绍
     */
    public function getTeacher()
    {
        $teacher_id = $this->request->post('id');
        $model = new PersonalM();
        $data = $model->getTeacherInfo($teacher_id);
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
    /*
     * 获取我的优惠券接口
     */
    public function myCoupon()
    {

        $status = $this->request->post('status')?:0;
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $model = new PersonalM();
        $data = $model->setUserId(UID)->setOrgCateId(ORG_CATE_ID)->setStatus($status)->setPage($page)->setPagesize($pagesize)->getCouponList();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
    /*
     * 获取我的课程接口
     */
    public function myCourse()
    {
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $model = new PersonalM();
        $data = $model->setUserId(UID)->setOrgCateId(ORG_CATE_ID)->setPage($page)->setPagesize($pagesize)->getCourseList();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
    /*
     * 获取我的积分接口
     */
    public function myScore()
    {
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $model = new PersonalM();
        $data = $model->setUserId(UID)->setOrgCateId(ORG_CATE_ID)->setPage($page)->setPagesize($pagesize)->getScoreList();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }

    /*
     * 获取我的听课历史接口
     */
    public function myCourseHistory()
    {
        $page = $this->request->post('page');
        $pagesize = $this->request->post('pagesize');
        $model = new PersonalM();
        $data = $model->setUserId(UID)->setOrgCateId(ORG_CATE_ID)->setPage($page)->setPagesize($pagesize)->getCourseHistoryList();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }

    /**
     * 获取优惠券接口
     */
    public function coupon()
    {
        $model = new PersonalM();
        $data = $model->setUserId(UID)->getCoupon();
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }

    /**
     * 领取优惠券接口
     */
    public function getcoupon()
    {
        $id = $this->request->post('id')?:0;
        if($id <= 0){
            $this->error('优惠券编号错误！');
        }
        $model = new PersonalM();
        $data['coupon_id']= $id;
        $data['user_id']= UID;
        $data['org_cate_id']= ORG_CATE_ID;
        $data['create_time']= time();
        $user_coupon = Db::name('coupon_user')->where('user_id','=',$data['user_id'])->where('org_cate_id','=',ORG_CATE_ID)->where('coupon_id','=',$data['coupon_id'])->find();
        if($user_coupon){
            $this->error('已经领取过该优惠券了');
        }
        $res = $model->saveCoupon($data);
        if($res){
            $this->success('领取成功！');
        }else{
            $this->error('领取失败！');
        }
    }

    /**
     * 领取优惠券接口
     */
    public function couponinfo()
    {
        $id = $this->request->post('id')?:0;
        if($id <= 0){
            $this->error('优惠券编号错误！');
        }
        $model = new PersonalM();
        $data = $model->couponinfo($id);
        if($data){
            $this->success('', $data);
        }else{
            $this->error('暂无数据');
        }
    }
}